package com.iflytek.aidemo.student;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.iflytek.aidemo.R;
import com.iflytek.aidemo.model.WrongQuestionRecord;
import java.util.List;
import java.util.ArrayList;

public class WrongQuestionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    // items 包含两种类型：String(日期标题) 或 WrongQuestionRecord(条目)
    private final List<Object> items = new ArrayList<>();

    public WrongQuestionAdapter(List<Object> groupedItems) {
        if (groupedItems != null) {
            items.addAll(groupedItems);
        }
    }

    public void updateData(List<Object> groupedItems) {
        items.clear();
        if (groupedItems != null) items.addAll(groupedItems);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return (items.get(position) instanceof String) ? TYPE_HEADER : TYPE_ITEM;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (viewType == TYPE_HEADER) {
            View v = inflater.inflate(R.layout.item_date_header, parent, false);
            return new HeaderHolder(v);
        } else {
            View v = inflater.inflate(R.layout.item_wrong_question, parent, false);
            return new ItemHolder(v);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Object obj = items.get(position);
        if (holder instanceof HeaderHolder) {
            ((HeaderHolder) holder).tvHeader.setText((String) obj);
        } else if (holder instanceof ItemHolder) {
            WrongQuestionRecord record = (WrongQuestionRecord) obj;
            ((ItemHolder) holder).tvAnswer.setText("正确答案: " + record.getQuestion() + " = " + record.getAnswer());
            Glide.with(holder.itemView.getContext())
                    .load(record.getWrongImagePath())
                    .placeholder(R.drawable.ic_image_placeholder)
                    .into(((ItemHolder) holder).ivWrongImage);
        }
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    static class HeaderHolder extends RecyclerView.ViewHolder {
        TextView tvHeader;
        HeaderHolder(@NonNull View itemView) {
            super(itemView);
            tvHeader = itemView.findViewById(R.id.tv_date_header);
        }
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        ImageView ivWrongImage;
        TextView tvAnswer;
        ItemHolder(View itemView) {
            super(itemView);
            ivWrongImage = itemView.findViewById(R.id.iv_wrong_image);
            tvAnswer = itemView.findViewById(R.id.tv_answer);
        }
    }
}